package cn.sieis.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sieis.pojo.Contest;
import cn.sieis.pojo.Project;
import cn.sieis.pojo.School;
import cn.sieis.pojo.Status;
import cn.sieis.service.contest.ContestService;
import cn.sieis.service.project.ProjectService;
import cn.sieis.service.school.SchoolService;
import cn.sieis.service.status.StatusService;
import cn.sieis.tools.Constants;
import cn.sieis.tools.PageSupport;

@Controller
@RequestMapping("/back/project")
public class ProjectController {


	@Resource
	private ProjectService projectService;
	
	@Resource
	private StatusService statusService;

	@Resource
	private SchoolService schoolService;
	
	@Resource
	private ContestService contestService;

	@RequestMapping(value = "/list.html")
	public String getProjectList(Model model,
			@RequestParam(value = "queryProName", required = false) String queryProName,
			@RequestParam(value = "queryProStatus", required = false) String queryProStatus,
			@RequestParam(value = "queryProSchool", required = false) String queryProSchool,
			@RequestParam(value = "queryProContest", required = false) String queryProContest,
			@RequestParam(value = "pageIndex", required = false) String pageIndex) {
		Integer _queryProStatus=null;
		Integer _queryProSchool = null;
		Integer _queryProContest = null;
		List<Project> projectList = null;
		List<Status> statusList=null;
		List<School> schoolList = null;
		List<Contest> contestList=null;
		// ����ҳ������
		int pageSize = Constants.pageSize;
		// ��ǰҳ��
		int currentPageNo = 1;

		if (queryProName == null) {
			queryProName = "";
		}
		if (queryProStatus != null && !queryProStatus.equals("")) {
			_queryProStatus = Integer.parseInt(queryProStatus);
		}
		if (queryProSchool != null && !queryProSchool.equals("")) {
			_queryProSchool = Integer.parseInt(queryProSchool);
		}
		if (queryProContest != null && !queryProContest.equals("")) {
			_queryProContest = Integer.parseInt(queryProContest);
		}

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (NumberFormatException e) {
				return "redirect:/syserror.html";
			}
		}

		int totalCount = 0;
		try {
			totalCount = projectService.getProjectCount(queryProName,_queryProStatus, _queryProSchool, _queryProContest);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		try {
			projectList = projectService.getProjectList(queryProName,_queryProStatus, _queryProSchool, _queryProContest, currentPageNo,
					pageSize);
			statusList=statusService.getStatusList();
			schoolList = schoolService.getSchoolList();
			contestList=contestService.getConConList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("projectList", projectList);
		model.addAttribute("statusList", statusList);
		model.addAttribute("schoolList", schoolList);
		model.addAttribute("contestList", contestList);
		model.addAttribute("queryProName", queryProName);
		model.addAttribute("queryProStatus", queryProStatus);
		model.addAttribute("queryProSchool", queryProSchool);
		model.addAttribute("queryProContest", queryProContest);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "back/projectlist";
	}

	@RequestMapping(value = "/add.html", method = RequestMethod.GET)
	public String addProject(@ModelAttribute("project") Project project) {
		return "back/projectadd";
	}

	@RequestMapping(value = "/addsave.html", method = RequestMethod.POST)
	public String addProjectSave(Project project, HttpSession session, HttpServletRequest request) {
		boolean flag = true;
		if (flag) {
			try {
				if (projectService.addProject(project)) {
					return "redirect:/back/project/list.html";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "back/projectadd";
	}

	@RequestMapping(value = "/modifyproject/{id}", method = RequestMethod.GET)
	public String getProjectById(@PathVariable String id, Model model, HttpServletRequest request) {
		Project project = new Project();
		try {
			project = projectService.getProjectById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute(project);
		return "back/projectmodify";
	}

	@RequestMapping(value = "/modifysave.html", method = RequestMethod.POST)
	public String modifyProjectSave(Project project, HttpSession session, HttpServletRequest request) {
		boolean flag = true;
		if (flag) {
			try {
				if (projectService.modify(project)) {
					return "redirect:/back/project/list.html";
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "back/projectmodify";
	}

	@RequestMapping(value = "/delproject.html", method = RequestMethod.GET)
	public String delProject(@RequestParam("id") String id) {
		try {
			if (projectService.deleteProjectById(Integer.parseInt(id))) {
				System.out.println("ɾ���ɹ�");
			} else {
				System.out.println("ɾ��ʧ��");
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:/back/project/list.html";
	}

	@RequestMapping(value = "/prolist.html")
	public String getProList(Model model,
			@RequestParam(value = "queryProName", required = false) String queryProName,
			@RequestParam(value = "queryProStatus", required = false) String queryProStatus,
			@RequestParam(value = "queryProSchool", required = false) String queryProSchool,
			@RequestParam(value = "queryProContest", required = false) String queryProContest,
			@RequestParam(value = "pageIndex", required = false) String pageIndex) {
		Integer _queryProStatus=null;
		Integer _queryProSchool = null;
		Integer _queryProContest = null;
		List<Project> projectList = null;
		List<Status> statusList=null;
		List<School> schoolList = null;
		List<Contest> contestList=null;
		int pageSize = Constants.pageSize;
		// ��ǰҳ��
		int currentPageNo = 1;

		if (queryProName == null) {
			queryProName = "";
		}
		if (queryProStatus != null && !queryProStatus.equals("")) {
			_queryProStatus = Integer.parseInt(queryProStatus);
		}
		if (queryProSchool != null && !queryProSchool.equals("")) {
			_queryProSchool = Integer.parseInt(queryProSchool);
		}
		if (queryProContest != null && !queryProContest.equals("")) {
			_queryProContest = Integer.parseInt(queryProContest);
		}

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (NumberFormatException e) {
				return "redirect:/syserror.html";
			}
		}

		// ����������
		int totalCount = 0;
		try {
			totalCount = projectService.getProjectCount(queryProName,_queryProStatus, _queryProSchool, _queryProContest);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// ��ҳ��
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount = pages.getTotalPageCount();
		// ������ҳ��βҳ
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}
		try {
			projectList = projectService.getProList(queryProName, _queryProStatus, _queryProSchool, _queryProContest);
			statusList=statusService.getStatusList();
			schoolList = schoolService.getSchoolList();
			contestList=contestService.getConConList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("projectList", projectList);
		model.addAttribute("statusList", statusList);
		model.addAttribute("schoolList", schoolList);
		model.addAttribute("contestList", contestList);
		model.addAttribute("queryProName", queryProName);
		model.addAttribute("queryProStatus", queryProStatus);
		model.addAttribute("queryProSchool", queryProSchool);
		model.addAttribute("queryProContest", queryProContest);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "back/projectlist";
	}

	@RequestMapping(value = "/viewproject/{id}", method = RequestMethod.GET)
	public String getProjectByIdView(@PathVariable String id, Model model, HttpServletRequest request) {
		Project project = new Project();
		try {
			project = projectService.getProjectById(Integer.parseInt(id));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute(project);
		return "back/projectview";
	}

}
